#!/usr/bin/env bash

# Test that :task syntax only works in monorepos

# Test 1: Outside monorepo, :build should NOT work
cat <<'EOF' >mise.toml
[tasks.build]
run = 'echo "build task"'

[tasks."project:build"]
run = 'echo "project:build task"'
EOF

# Should fail with helpful error about monorepo setup
assert_fail "mise run ':build'" "require a monorepo root configuration"

# Test 2: In monorepo with experimental mode, :build SHOULD work
export MISE_EXPERIMENTAL=1

cat <<'EOF' >mise.toml
experimental_monorepo_root = true

[tasks.build]
run = 'echo "root build task"'
EOF

mkdir -p project
cat <<'EOF' >project/mise.toml
[tasks.build]
run = 'echo "project build task"'
EOF

mkdir -p app
cat <<'EOF' >app/mise.toml
[tasks.build]
run = 'echo "app build task"'
EOF

# Running ':build' from root should match root-level build task (not monorepo tasks)
assert_contains "mise run ':build'" "root build task"

# Should NOT run project:build or app:build from root
OUTPUT_ROOT=$(mise run ':build')
if echo "$OUTPUT_ROOT" | grep -E "project build task|app build task"; then
	echo "ERROR: 'mise run :build' from root incorrectly matched child tasks"
	exit 1
fi

# Running ':build' from project dir should only match //project:build
(
	cd project
	assert_contains "mise run ':build'" "project build task"

	# Should NOT run app:build when we're in project directory
	OUTPUT=$(mise run ':build')
	if echo "$OUTPUT" | grep -q "app build task"; then
		echo "ERROR: 'mise run :build' from project dir incorrectly matched //app:build"
		exit 1
	fi
)

# Running ':build' from app dir should only match //app:build
(
	cd app
	assert_contains "mise run ':build'" "app build task"

	# Should NOT run project:build when we're in app directory
	OUTPUT2=$(mise run ':build')
	if echo "$OUTPUT2" | grep -q "project build task"; then
		echo "ERROR: 'mise run :build' from app dir incorrectly matched //project:build"
		exit 1
	fi
)

# Test 3: Verify that 'mise run build' does NOT match monorepo tasks
unset MISE_EXPERIMENTAL

cat <<'EOF' >mise.toml
[tasks.build]
run = 'echo "root build"'

[tasks."project:build"]
run = 'echo "project build"'
EOF

# Should only run 'build', not 'project:build'
OUTPUT2=$(mise run build)

assert_contains "mise run build" "root build"

if echo "$OUTPUT2" | grep -q "project build"; then
	echo "ERROR: 'mise run build' incorrectly matched 'project:build'"
	exit 1
fi
